﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DVSProject.Lib;
using DVSProject.Models;

namespace DVSProject.Controllers
{
    public class MechanicalRemarkController : Controller
    {
        //
        // GET: /MechanicalRemark/

        public ActionResult Index()
        {
            if (Session["User"] == null) { return RedirectToAction("LogOn", "Account"); }
            MechanicalRemarkRepo _MechanicalRemarkRepo = new MechanicalRemarkRepo();
            IEnumerable<MechanicalRemarkModels> _MechanicalRemarks = _MechanicalRemarkRepo.GetMechanicalRemark();
            return View(_MechanicalRemarks);
        }
        //
        // GET: /MechanicalRemark/Details/5

        public ActionResult Details(int id)
        {
            if (Session["User"] == null) { return RedirectToAction("LogOn", "Account"); }
            MechanicalRemarkRepo _MechanicalRemarkRepo = new MechanicalRemarkRepo();
            MechanicalRemarkModels _MechanicalRemarkModels = new MechanicalRemarkModels();
            _MechanicalRemarkModels = _MechanicalRemarkRepo.GetMechanicalRemarkById(id);
            return View(_MechanicalRemarkModels);
        }

        //
        // GET: /MechanicalRemark/Create

        public ActionResult Create()
        {
            if (Session["User"] == null) { return RedirectToAction("LogOn", "Account"); }
            MechanicalRemarkModels _MechanicalRemarkModels = new MechanicalRemarkModels();
            TitleMechanicalRepo _TitleMechanicalRepo = new TitleMechanicalRepo();
            IEnumerable<TitleMechanicalModels> _TitleMechanicals = _TitleMechanicalRepo.GetTitleMechanicalDropDown();
            string selectedId = "อุปกรณ์";
            IEnumerable<SelectListItem> selectlist = from s in _TitleMechanicals
                                                     select new SelectListItem
                                                     {
                                                         Selected = (s.TitleMechanicalType == selectedId),
                                                         Text = s.TitleMechanicalType,
                                                         Value = s.TitleMechanicalType.ToString()
                                                     };

            _MechanicalRemarkModels.TitleSelect = selectlist;
            return View(_MechanicalRemarkModels);
        } 

        //
        // POST: /MechanicalRemark/Create

        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            if (Session["User"] == null) { return RedirectToAction("LogOn", "Account"); }
            try
            {
                // TODO: Add insert logic here
                MechanicalRemarkRepo _MechanicalRemarkRepo = new MechanicalRemarkRepo();
                MechanicalRemarkModels _MechanicalRemark = new MechanicalRemarkModels();

                _MechanicalRemark.TitleMechanicalType = collection["TitleMechanicalType"];
                _MechanicalRemark.MechanicalRemarkTh = collection["MechanicalRemarkTh"];
                _MechanicalRemark.MechanicalRemarkEng = collection["MechanicalRemarkEng"];
                _MechanicalRemark.CreateBy = (string)Session["User"];
                bool result = _MechanicalRemarkRepo.Create(_MechanicalRemark);
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
        
        //
        // GET: /MechanicalRemark/Edit/5
 
        public ActionResult Edit(int id)
        {
            if (Session["User"] == null) { return RedirectToAction("LogOn", "Account"); }
            MechanicalRemarkRepo _MechanicalRemarkRepo = new MechanicalRemarkRepo();
            MechanicalRemarkModels _Mechanical = new MechanicalRemarkModels();
            _Mechanical = _MechanicalRemarkRepo.GetMechanicalRemarkById(id);

            TitleMechanicalRepo _TitleMechanicalRepo = new TitleMechanicalRepo();
            IEnumerable<TitleMechanicalModels> _TitleMechanicals = _TitleMechanicalRepo.GetTitleMechanicalDropDown();
            string selectedId = "อุปกรณ์";
            IEnumerable<SelectListItem> selectlist = from s in _TitleMechanicals
                                                     select new SelectListItem
                                                     {
                                                         Selected = (s.TitleMechanicalType == selectedId),
                                                         Text = s.TitleMechanicalType,
                                                         Value = s.TitleMechanicalType.ToString()
                                                     };

            _Mechanical.TitleSelect = selectlist;
            return View(_Mechanical);
        }

        //
        // POST: /MechanicalRemark/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            if (Session["User"] == null) { return RedirectToAction("LogOn", "Account"); }
            try
            {
                // TODO: Add update logic here

                MechanicalRemarkRepo _MechanicalRemarkRepo = new MechanicalRemarkRepo();
                MechanicalRemarkModels _MechanicalRemark = new MechanicalRemarkModels();
                _MechanicalRemark.Id = id;
                _MechanicalRemark.TitleMechanicalType = collection["TitleMechanicalType"];
                _MechanicalRemark.MechanicalRemarkTh = collection["MechanicalRemarkTh"];
                _MechanicalRemark.MechanicalRemarkEng = collection["MechanicalRemarkEng"];
                _MechanicalRemark.UpdateBy = (string)Session["User"];
                bool result = _MechanicalRemarkRepo.Edit(_MechanicalRemark);
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /MechanicalRemark/Delete/5
 
        public ActionResult Delete(int id)
        {
            if (Session["User"] == null) { return RedirectToAction("LogOn", "Account"); }
            MechanicalRemarkRepo _MechanicalRemarkRepo = new MechanicalRemarkRepo();
            MechanicalRemarkModels _MechanicalRemark = new MechanicalRemarkModels();
            _MechanicalRemark = _MechanicalRemarkRepo.GetMechanicalRemarkById(id);
            return View(_MechanicalRemark);
        }

        //
        // POST: /MechanicalRemark/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            if (Session["User"] == null) { return RedirectToAction("LogOn", "Account"); }
            try
            {
                // TODO: Add delete logic here
                MechanicalRemarkRepo _MechanicalRemarkRepo = new MechanicalRemarkRepo();
                MechanicalRemarkModels _MechanicalRemark = new MechanicalRemarkModels();
                _MechanicalRemark.Id = id;
                _MechanicalRemark.UpdateBy = (string)Session["User"];
                bool result = _MechanicalRemarkRepo.Delete(_MechanicalRemark);
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
